package com.feng.service.impl;

import com.feng.dao.OnlineDao;
import com.feng.dto.OnlineDto;
import com.feng.service.OnlineService;
import com.feng.util.StringUtil;
import com.feng.vo.PageVo;
import com.feng.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 丰
 * @date: 2023/1/1 16:14
 */
@Service
public class OnlineServiceImpl implements OnlineService {

    @Autowired
    private OnlineDao onlineDao;


    @Override
    public ResultVo<List<OnlineDto>> selectAll(PageVo pageVo) {
        StringUtil.checkPageVo(pageVo);
        // 查出数据封装
        List<OnlineDto> onlineDtos = onlineDao.selectAll(pageVo);
        ResultVo<List<OnlineDto>> resultVo = ResultVo.failed();
        if (onlineDtos != null){
            resultVo = ResultVo.success(onlineDtos);
            // 查出count
            resultVo.count(onlineDao.count(pageVo)).code(0);
        }
        return resultVo;
    }

    @Override
    public ResultVo<Integer> add(OnlineDto onlineDto) {
        // 获取时间
        String date = StringUtil.getStringDate();
        onlineDto.setCreateTime(date);
        onlineDto.setUpdateTime(date);
        // 调用dao
        int result = onlineDao.add(onlineDto);
        if (result > 0){
            return ResultVo.success(result);
        }
        return ResultVo.failed();
    }

    @Override
    public ResultVo<Integer> deleteById(int id) {
        int result = onlineDao.deleteById(id);
        if (result > 0){
            return ResultVo.success(result);
        }
        return ResultVo.failed();
    }
}
